Let us write a small code that mimics a dice by generating a random number between 1 and 6. It then asks you to enter a number between 1 and 6. If what you enter equals the dice value you win or else you loose.
In [1]:
#From the random library we import the randint function.
#Generate a random number between 1 and 6
#!!!Input always passes strings !!!
Now lets improve the previous function "dice()" such that the user has 5 attempts at entering the number and getting the right dice value.
In [3]:
Excercise : Can you edit the the previous code so that it stops asking the user to enter a number when the value entered matches the dice value ? Hint: You will need to use the "break" command within the while loop. Also remember that you can use the "return" command to pass values back to the calling function.
In [6]:
# Enter code here
Let us write a program that asks the user to enter a text sentence and counts the number of times a particular character occurs in the sentence.
In [4]:
Excercise: Can you use the for loop so that it counts the number of times a given word occurs in a sentence ? Hint: Use the split() command to split the sentence into a list of words and then use the for loop to traverse through the list.
In [ ]: